//go:build windows

package gateway_mac

import (
	"fmt"
	"os/exec"
	"strings"
	"syscall"
)

func discoverGatewayMacOSSpecific() (mac string, err error) {
	routeCmd := exec.Command("arp", "-a")
	routeCmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
	output, err := routeCmd.CombinedOutput()
	if err != nil {
		return "", err
	}
	mac_ip, err := DiscoverGateway()
	if err != nil {
		return "", err
	}

	arpTable := make(map[string]string)
	for _, line := range strings.Split(string(output), "\r\n") {
		m := strings.Fields(line)
		if len(m) != 3 {
			continue
		}
		arpTable[m[0]] = m[1]
	}

	if len(arpTable) == 0 {
		return "", fmt.Errorf("no arp table found")
	}

	//format is:
	//Internet 地址         物理地址              类型
	//192.168.1.101         f2-6a-1d-97-ef-9a     动态
	return arpTable[mac_ip.String()], nil
}
